﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.Net;

using AppPriceLive.Biz;
using AppPriceLive.Entities.Models;

namespace AppPriceLive.WindowsService
{
    public partial class AppPriceLiveService : ServiceBase
    {
        System.Timers.Timer timer;
        EventLog eventLog;
        public AppPriceLiveService()
        {
            InitializeComponent();

            // Reads settings from app.config
            this.ServiceName = ConfigurationSettings.AppSettings["serviceName"];
            string eventLogName = ConfigurationSettings.AppSettings["eventLogName"];
            string eventLogSource = ConfigurationSettings.AppSettings["eventLogSource"];

            // Initializes eventlog
            if (!EventLog.SourceExists(eventLogSource))
                EventLog.CreateEventSource(eventLogSource, eventLogName);

            eventLog = new EventLog();
            ((System.ComponentModel.ISupportInitialize)(eventLog)).BeginInit();

            eventLog.Source = eventLogSource;

            // Registers the application
            eventLog.Log = eventLogName;

            ((System.ComponentModel.ISupportInitialize)(eventLog)).EndInit();

            //UpdatePrice();
        }

        protected override void OnStart(string[] args)
        {
            eventLog.WriteEntry("+++++++++++++ START AppPriceLiveService +++++++++++++", EventLogEntryType.Warning);

            // Runs the process every X minutes
            int interval = Int32.Parse(ConfigurationSettings.AppSettings["interval"]) * 1000;
            timer = new System.Timers.Timer();
            timer.Interval = interval;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(UpdatePrice);
            timer.Start(); 
        }

        protected void UpdatePrice(object obj, System.Timers.ElapsedEventArgs e)
        {
            CurrencyInfoInfo currencyInfoInfo = new CurrencyInfoInfo();
            CurrencyInfoService currencyInfoService = new CurrencyInfoService();

            #region Vàng thế giới
            try
            {
                string[] s1 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlWorldGold"]).Split('/');
                currencyInfoInfo.CurrencyListId = 1;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s1[0]);
                currencyInfoInfo.Ask = decimal.Parse(s1[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch{ }
            #endregion

            #region Bạc
            try
            {
                string[] s2 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlSilver"]).Split('/');
                currencyInfoInfo.CurrencyListId = 2;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s2[0]);
                currencyInfoInfo.Ask = decimal.Parse(s2[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Bạch kim
            try
            {
                string[] s3 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlPaltinum"]).Split('/');
                currencyInfoInfo.CurrencyListId = 3;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s3[0]);
                currencyInfoInfo.Ask = decimal.Parse(s3[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Dầu thô
            try
            {
                string[] s4 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlOid"]).Split('/');
                currencyInfoInfo.CurrencyListId = 4;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s4[0]);
                currencyInfoInfo.Ask = decimal.Parse(s4[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region SJC Hà Nội
            try
            {
                string[] s5 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlSjc_hanoi"]).Split('/');
                currencyInfoInfo.CurrencyListId = 5;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s5[0]);
                currencyInfoInfo.Ask = decimal.Parse(s5[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region SJC TP HCM
            try
            {
                string[] s6 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlSjc_hcm"]).Split('/');
                currencyInfoInfo.CurrencyListId = 6;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s6[0]);
                currencyInfoInfo.Ask = decimal.Parse(s6[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region SJC Tự do
            try
            {
                string[] s7 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlSjc_preemkt"]).Split('/');
                currencyInfoInfo.CurrencyListId = 7;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s7[0]);
                currencyInfoInfo.Ask = decimal.Parse(s7[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Eximbank
            try
            {
                string[] s8 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlEximbank"]).Split('/');
                currencyInfoInfo.CurrencyListId = 8;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s8[0]);
                currencyInfoInfo.Ask = decimal.Parse(s8[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Sacombank
            try
            {
                string[] s9 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlSacombank"]).Split('/');
                currencyInfoInfo.CurrencyListId = 9;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s9[0]);
                currencyInfoInfo.Ask = decimal.Parse(s9[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Á Châu Bank
            try
            {
                string[] s10 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlAsia"]).Split('/');
                currencyInfoInfo.CurrencyListId = 10;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s10[0]);
                currencyInfoInfo.Ask = decimal.Parse(s10[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Vàng Rồng Thăng Long
            try
            {
                string[] s11 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlBtmc"]).Split('/');
                currencyInfoInfo.CurrencyListId = 11;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s11[0]);
                currencyInfoInfo.Ask = decimal.Parse(s11[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region Thần Tài Sacombank
            try
            {
                string[] s12 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlSbj"]).Split('/');
                currencyInfoInfo.CurrencyListId = 12;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s12[0]);
                currencyInfoInfo.Ask = decimal.Parse(s12[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region EUR/USD
            try
            {
                string[] s13 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlEur_usd"]).Split('/');
                currencyInfoInfo.CurrencyListId = 13;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s13[0]);
                currencyInfoInfo.Ask = decimal.Parse(s13[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region GBP/USD
            try
            {
                string[] s14 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlGbp_usd"]).Split('/');
                currencyInfoInfo.CurrencyListId = 14;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s14[0]);
                currencyInfoInfo.Ask = decimal.Parse(s14[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region TPY/USD
            try
            {
                string[] s15 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlTpy_usd"]).Split('/');
                currencyInfoInfo.CurrencyListId = 15;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s15[0]);
                currencyInfoInfo.Ask = decimal.Parse(s15[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

            #region USD
            try
            {
                string[] s16 = new WebClient().DownloadString(ConfigurationSettings.AppSettings["urlUsd"]).Split('/');
                currencyInfoInfo.CurrencyListId = 16;
                currencyInfoInfo.UpdateTime = DateTime.Now;
                currencyInfoInfo.Bid = decimal.Parse(s16[0]);
                currencyInfoInfo.Ask = decimal.Parse(s16[1]);
                currencyInfoService.Update_BigAndAsk(currencyInfoInfo);
            }
            catch { }
            #endregion

        }

        protected override void OnStop()
        {
            eventLog.WriteEntry("+++++++++++++ STOP AppPriceLiveService +++++++++++++", EventLogEntryType.Warning);
        }
    }
}
